package com.music.mysongs.utils

import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.music.mysongs.R
import com.music.mysongs.helper.myCxtReadIntSPF
import com.music.mysongs.helper.myCxtWriteSPF
import com.music.mysongs.receiver.OffLineReceiver

open class BaseActivity : AppCompatActivity() {
    private lateinit var offLine: OffLineReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        when (myCxtReadIntSPF(baseContext, "spfTheme")) {
            -1 -> {
                myLog("BaseActivity: defaultTheme")
                myCxtWriteSPF(baseContext, "spfTheme", null, 0)
            }
            0 -> {
                setTheme(R.style.BlueTheme)
            }
            1 -> {
                setTheme(R.style.CyanTheme)
            }
            2 -> {
                setTheme(R.style.PinkTheme)
            }
            3 -> {
                setTheme(R.style.RedTheme)
            }
            4 -> {
                setTheme(R.style.GreenTheme)
            }
        }
        ActivityCollector.addActivity(this)
    }

    override fun onResume() {
        super.onResume()
        supportActionBar?.hide()
        /*supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
        supportActionBar?.setCustomView(R.layout.layout_activity_topbar)*/
        //注册下线广播/换主题
        val intentFilterOffLine = IntentFilter()
        intentFilterOffLine.addAction("com.music.mysongs.OFFLINE")
        intentFilterOffLine.addAction("com.music.mysongs.THEME")
        offLine = OffLineReceiver()
        registerReceiver(offLine, intentFilterOffLine)
    }

    override fun onPause() {
        super.onPause()
        unregisterReceiver(offLine)
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}